package com.zrrd.leetcode;

public class FindMaxValueOfEquation {
    public int findMaxValueOfEquation(int[][] points, int k) {
        int result = Integer.MIN_VALUE;
        for (int i = 0, j = 1; j < points.length; j++) {
            while (points[j][0] - points[i][0] > k && i < j) i++;
            while (points[j][0] - points[i][0] <= k && i < j) {
                int tmp = points[j][0] - points[i][0] + points[i][1] + points[j][1];
                if (tmp > result) result = tmp;
                i++;
            }
        }
        return result;
    }

    public static void main(String[] args) {
        int[][] points = {{-19, -12}, {-13, -18}, {-12, 18}, {-11, -8}, {-8, 2}, {-7, 12}, {-5, 16}, {-3, 9}, {1, -7}, {5, -4}, {6, -20}, {10, 4}, {16, 4}, {19, -9}, {20, 19}};
        int k = 6;
        FindMaxValueOfEquation find = new FindMaxValueOfEquation();
        int result = find.findMaxValueOfEquation(points, k);
        System.out.println(result);

    }
}
